package co.yixiang.config;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static me.chanjar.weixin.common.api.WxConsts.EventType.SUBSCRIBE;
import static me.chanjar.weixin.common.api.WxConsts.EventType.UNSUBSCRIBE;
import static me.chanjar.weixin.common.api.WxConsts.MenuButtonType.CLICK;
import static me.chanjar.weixin.common.api.WxConsts.MenuButtonType.VIEW;
import static me.chanjar.weixin.common.api.WxConsts.XmlMsgType.EVENT;

/**
 * wechat mp configuration
 */
@AllArgsConstructor
@Configuration
@EnableConfigurationProperties(WxMpProperties.class)
public class WxMpConfiguration {
//    private final LogHandler logHandler;
//    private final NullHandler nullHandler;
//    private final KfSessionHandler kfSessionHandler;
//    private final StoreCheckNotifyHandler storeCheckNotifyHandler;
//    private final LocationHandler locationHandler;
//    private final MenuHandler menuHandler;
//    private final MsgHandler msgHandler;
//    private final UnsubscribeHandler unsubscribeHandler;
//    private final SubscribeHandler subscribeHandler;
//    private final ScanHandler scanHandler;
//    private final WxMpProperties properties;
//    private final RedisHandler redisHandler;

    @Bean
    public WxMpService wxMpService() {

        final List<WxMpProperties.MpConfig> configs = new ArrayList<>();
        WxMpProperties.MpConfig mpConfig = new WxMpProperties.MpConfig();
//        String appId = redisHandler.getVal("wechat_appid");
//        String secret = redisHandler.getVal("wechat_appsecret");
//        String token = redisHandler.getVal("wechat_token");
//        String aesKey = redisHandler.getVal("wechat_encodingaeskey");

        mpConfig.setAppId("wxc8ac2223561b2b21");
        mpConfig.setSecret("a31e2ecdcc253f2916783ef0eeb0ddb5");
        mpConfig.setToken("hyhw2020");
        mpConfig.setAesKey("Au6q9t8cnRkgrJBhbDGu4UTFRxPKHKtc1V2h7DhEuFC");
        configs.add(mpConfig);

//
//        if(StrUtil.isNotBlank(appId) && StrUtil.isNotBlank(secret)
//                && StrUtil.isNotBlank(token) && StrUtil.isNotBlank(aesKey)) {
//            mpConfig.setAppId(appId);
//            mpConfig.setSecret(secret);
//            mpConfig.setToken(token);
//            mpConfig.setAesKey(aesKey);
//            System.out.println(mpConfig);
//
//            configs.add(mpConfig);
//        }else{
//
//        }

        System.out.println("WxMpService===================================>configs:"+ JSON.toJSONString(configs));

        if (configs.isEmpty()) {
            throw new RuntimeException("请先配置！");
        }

        WxMpService service = new WxMpServiceImpl();
        service.setMultiConfigStorages(configs
            .stream().map(a -> {
                WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
                configStorage.setAppId(a.getAppId());
                configStorage.setSecret(a.getSecret());
                configStorage.setToken(a.getToken());
                configStorage.setAesKey(a.getAesKey());
                return configStorage;
            }).collect(Collectors.toMap(WxMpDefaultConfigImpl::getAppId, a -> a, (o, n) -> o)));
        return service;
    }

//    @Bean
//    public WxMpMessageRouter messageRouter(WxMpService wxMpService) {
//        final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService);
//
//        // 记录所有事件的日志 （异步执行）
//        //newRouter.rule().handler(this.logHandler).next();
//
//        // 接收客服会话管理事件
////        newRouter.rule().async(false).msgType(EVENT).event(KF_CREATE_SESSION)
////            .handler(this.kfSessionHandler).end();
////        newRouter.rule().async(false).msgType(EVENT).event(KF_CLOSE_SESSION)
////            .handler(this.kfSessionHandler).end();
////        newRouter.rule().async(false).msgType(EVENT).event(KF_SWITCH_SESSION)
////            .handler(this.kfSessionHandler).end();
//
//        // 门店审核事件
//        //newRouter.rule().async(false).msgType(EVENT).event(POI_CHECK_NOTIFY).handler(this.storeCheckNotifyHandler).end();
//
//        // 自定义菜单事件
//        newRouter.rule().async(false).msgType(EVENT).event(CLICK).handler(this.menuHandler).end();
//
//        // 点击菜单连接事件
//        newRouter.rule().async(false).msgType(EVENT).event(VIEW).handler(this.nullHandler).end();
//
//        // 关注事件
//        newRouter.rule().async(false).msgType(EVENT).event(SUBSCRIBE).handler(this.subscribeHandler).end();
//
//        // 取消关注事件
//        newRouter.rule().async(false).msgType(EVENT).event(UNSUBSCRIBE).handler(this.unsubscribeHandler).end();
//
//        // 上报地理位置事件
//        //newRouter.rule().async(false).msgType(EVENT).event(EventType.LOCATION).handler(this.locationHandler).end();
//
//        // 接收地理位置消息
//        //newRouter.rule().async(false).msgType(XmlMsgType.LOCATION).handler(this.locationHandler).end();
//
//        // 扫码事件
//        //newRouter.rule().async(false).msgType(EVENT).event(EventType.SCAN).handler(this.scanHandler).end();
//
//        // 默认
//        newRouter.rule().async(false).handler(this.msgHandler).end();
//
//        return newRouter;
//    }

}
